package com.pizidea.imagepicker.ui;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.efounder.builder.base.data.EFDataSet;
import com.efounder.builder.base.data.EFRowSet;
import com.efounder.builder.base.data.ESPRowSet;
import com.efounder.constant.EnvironmentVariable;
import com.efounder.eai.EAI;
import com.efounder.eai.data.JParamObject;
import com.efounder.eai.data.JResponseObject;
import com.efounder.form.EFFormDataModel;
import com.efounder.frame.baseui.BaseFragment;
import com.efounder.mobilecomps.R;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.efounder.frame.utils.Constants.KEY_SETTING_APPID;
import static com.efounder.frame.utils.Constants.KEY_SETTING_DEFAULT_SERVER_ADDRESS;
import static com.efounder.frame.utils.Constants.KEY_SETTING_DEFAULT_SERVER_HTTP_TYPE;
import static com.efounder.frame.utils.Constants.KEY_SETTING_DEFAULT_SERVER_PORT;


public class WorkOrderPeopleDetailFragment extends BaseFragment {

    private ListView mListView;
    private Context mContext;
    private List<ESPRowSet> rowSetList1;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        loadDataFromNet();
        mContext = getActivity();
        View view = inflater.inflate(R.layout.fragment_work_order_people_detail, container, false);
        mListView = (ListView) view.findViewById(R.id.lv_people_detail);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            }
        });
        return view;
    }

    class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return rowSetList1.size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ViewHolder viewHolder;
            if (null == view) {
                view = View.inflate(mContext, R.layout.item_work_order_detail, null);
                viewHolder = new ViewHolder();
                viewHolder.tvName = (TextView) view.findViewById(R.id.order_detail_name);
                viewHolder.tvTitle = (TextView) view.findViewById(R.id.order_detail_title);
                viewHolder.tvPhone = (TextView) view.findViewById(R.id.order_detail_phonenum);
                viewHolder.tvSignInTime = (TextView) view.findViewById(R.id.order_detail_sign_in_time);
                viewHolder.tvSignInAddress = (TextView) view.findViewById(R.id.order_detail_sign_in_address);
                viewHolder.tvSignOutTime = (TextView) view.findViewById(R.id.order_detail_sign_out_time);
                viewHolder.tvSignOutAddress = (TextView) view.findViewById(R.id.order_detail_sign_out_address);
                view.setTag(viewHolder);
            } else {
                viewHolder = (ViewHolder) view.getTag();
            }
            EFRowSet rowSet = (EFRowSet) rowSetList1.get(i);
            Map<String, Object> dataMap = rowSet.getDataMap();
            String name = (String) dataMap.get("GDPGITEM.F_RYMC");
            String phone = (String) dataMap.get("GDPGITEM.F_PHONE");
            String title = (String) dataMap.get("GDPGITEM.F_RYLX");
            if ("1".equals(title)) {
                viewHolder.tvTitle.setText("组长");
            } else if ("2".equals(title)) {
                viewHolder.tvTitle.setText("组员");
            }

            viewHolder.tvName.setText(name);
            viewHolder.tvPhone.setText(phone);
            viewHolder.tvSignInTime.setText("2017-03-17 22:00");
            viewHolder.tvSignInAddress.setText("中国山东省历城区*********");
            viewHolder.tvSignOutTime.setText("2017-03-17 22:00");
            viewHolder.tvSignOutAddress.setText("中国山东省历城区*********");
            return view;
        }

        private class ViewHolder {
            TextView tvName;
            TextView tvTitle;
            TextView tvPhone;
            TextView tvSignInTime;
            TextView tvSignInAddress;
            TextView tvSignOutTime;
            TextView tvSignOutAddress;
        }
    }

    private void loadDataFromNet() {
        /*
         * 内部AsyncTask类
        */
        class GetTaskDataAsyncTask extends AsyncTask<String, Integer, EFFormDataModel> {
            long time = System.currentTimeMillis();

            // 进度显示
            protected void onPreExecute() {
                // 进度显示
                super.onPreExecute();
                LoadingDataUtilBlack.show(getActivity());
            }

            @Override
            protected EFFormDataModel doInBackground(String... params) {
//                SystemClock.sleep(2000); //用于测试“正在加载”加载框
                EAI.Protocol = EnvironmentVariable.getProperty(KEY_SETTING_DEFAULT_SERVER_HTTP_TYPE);
                EAI.Server = EnvironmentVariable.getProperty(KEY_SETTING_DEFAULT_SERVER_ADDRESS);
                EAI.Port = EnvironmentVariable.getProperty(KEY_SETTING_DEFAULT_SERVER_PORT);
                EAI.Path = "OSPMobile";
                EAI.Service = "Android";
                JParamObject PO = JParamObject.Create();
                PO.setEnvValue("DBNO", EnvironmentVariable.getProperty("sign"));// 数据标示
                PO.setEnvValue("Product", EnvironmentVariable.getProperty(KEY_SETTING_APPID));// 服务名称
                // 防止盗用
                PO.setEnvValue(KEY_SETTING_APPID, "PGYDYYM");
                // 权限管理
                PO.SetValueByParamName("LOAD_GNQX", "1");
                JParamObject.assign(PO);
                JResponseObject RO = null;
                EFFormDataModel flowTaskDataSet = null;

                PO.SetValueByParamName("BILL_TJLX", "GUID");
                PO.SetValueByParamName("F_GUID", "bfbf0517-9e09-4817-a1c0-6011decf3cae");
                PO.SetValueByParamName("OBJ_GUID", "bfbf0517-9e09-4817-a1c0-6011decf3cae");
                PO.SetValueByParamName("MDL_ID", "GDPGModel");
                PO.SetValueByEnvName("ServiceKey", "ESPBillProvider");

                PO.setEnvValue("DBNO", "ZYHYGL01");
                PO.setEnvValue("DataBaseName", "ZYYT_DB01");

                try {
                    RO = (JResponseObject) EAI.DAL.IOM("BZServiceComponentManager",
                            "syncRunService", PO);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (RO != null)
                    flowTaskDataSet = (EFFormDataModel) RO.getResponseObject();
                return flowTaskDataSet;
            }

            // 当后台操作结束时，此方法将会被调用，计算结果将做为参数传递到此方法中，直接将结果显示到UI组件上
            protected void onPostExecute(EFFormDataModel result) {
//                LoadingDataUtil.dismiss();
                LoadingDataUtilBlack.dismiss();
                if (result == null) {
                    return;
                }
                MyAdapter myAdapter = new MyAdapter();
                EFRowSet efRowSet = (EFRowSet) result.getDataSetMap()
                        .get("GDPGHEAD")
                        .getRowSetList()
                        .get(0);
                rowSetList1 = efRowSet.getDataSetMap()
                        .get("GDPGITEM")
                        .getRowSetList();
                if (null != rowSetList1 && 0 != rowSetList1.size()) {
                    mListView.setAdapter(myAdapter);
                }
            }
        }
        GetTaskDataAsyncTask getTaskDataAsyncTask = new GetTaskDataAsyncTask();
        getTaskDataAsyncTask.execute();
    }

}
